objectActions = {}
mouseModeActions = {}
selected = {}
mouseMode = 1

function world() return game:editor():world() end

function showPartButtons(obj)
    game:editor():addActionButton(function()
        local t = world():getService("SpawnManager"):getAllSpawnParts()
        for i=1, #t do
            local p = t[i]
            local c = p:getChildren()
            for j=1, #c do
                if c[j]:isA("SpawnPoint") then c[j]:remove() end
            end
        end
        create("SpawnPoint").Parent = obj
    end, "setspawn.png", "Make new players spawn here")
end

function showTextureButton(obj)
    game:editor():addActionButton(function()
        game:editor():showInsertTextureDialog(obj)
    end, "addtexture.png", "Add an image to this "..obj.Type)
end

function showMouseModeButtons(m)
    if mouseModeActions[m] then
        mouseModeActions[m]()
    end
end
function showObjectButtons(obj)
    local t = obj.Type
    if objectActions[t] then
        objectActions[t](obj)
    end
end


link(game:editor().SelectionChanged, function(s)
    local c = #s
    game:editor():clearActionButtons()
    if c == 0 then
        showMouseModeButtons(mouseMode)
    elseif c == 1 then
        local obj = s[1]
        showObjectButtons(obj)
    else

    end
end)

link(game:editor().MouseModeChanged, function(m)
    mouseMode = m
    game:editor():clearActionButtons()
    if #selected == 0 then
        showMouseModeButtons(m)
    end
end)

objectActions["Terrain"] = function(obj)
    if game:editor():world():getChild("Terrain") == nil then
        game:editor():world():getService("Terrain")
        if game:editor():world():getService("World"):getChild("Ground") ~= nil then
            game:editor():world():getService("Terrain").Color = game:editor():world():getService("World"):getChild("Ground").Color
            game:editor():world():getService("World").Ground.Parent = game:editor():world():getService("Storage")
        end
    end
    game:editor():addActionButton(function()
        game:editor():world().Terrain:generate()
    end, "generate.png", "Generate random terrain")
end

objectActions["Box"] = function(obj) showPartButtons(obj); showTextureButton(obj); end
objectActions["Circle"] = function(obj) showPartButtons(obj); showTextureButton(obj); end
objectActions["Triangle"] = showPartButtons

mouseModeActions[13] = objectActions["Terrain"]
mouseModeActions[0] = function()
    game:editor():addActionButton(function()
        game:editor():showDefaultColorPicker()
    end, "setcolor.png", "Set default color")
end
mouseModeActions[3] = mouseModeActions[0]
mouseModeActions[9] = mouseModeActions[0]
mouseModeActions[10] = mouseModeActions[0]
mouseModeActions[12] = mouseModeActions[0]
mouseModeActions[15] = mouseModeActions[0]